<?php

namespace backend\models;

use Yii;

/**
 * This is the model class for table "message".
 *
 * @property string $id
 * @property string $title 标题
 * @property int $class 类型
 * @property string $content 内容
 * @property int $create_time 创建时间
 * @property int $user_id 0为所有用户的消息
 * @property int $is_read 1 已读   0未读
 * @property string $active_url 活动链接
 */
class MessageModel extends \backend\base\BaseModel
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'message';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['class', 'create_time', 'user_id', 'is_read','user_class'], 'integer'],
            [['content'], 'string'],
            [['title'], 'string', 'max' => 255],
            [['active_url'], 'string', 'max' => 100],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'title' => '标题',
            'class' => '类型',
            'content' => '内容',
            'create_time' => '创建时间',
            'user_id' => '用户',
            'is_read' => '是否读取',
            'active_url' => '活动链接',
        ];
    }

    /**
     * 保存前
     * @param bool $insert
     * @return bool
     */
    public function beforeSave($insert)
    {
        if(parent::beforeSave($insert)){
            $time = time();
            if($this->isNewRecord){
                $this->create_time = $time;
                $this->is_read = 0;
            }
            return true;
        }
        return false;
    }
}
